package com.zhxx.book.controller;

import com.zhxx.book.entity.Teacher;
import com.zhxx.book.entity.User;
import com.zhxx.book.entity.UserSay;
import com.zhxx.book.service.IUserSayService;
import com.zhxx.book.service.IUserService;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 学员说
 * @Author Alex
 * @Date 2022-11-24 15:12
 */
@RestController
@RequestMapping("/user/say")
public class UserSayController {
   @Autowired
   private IUserSayService userSayService;
   @Autowired
   private IUserService userService;

   @GetMapping
   public List<UserSay> list() {
      List<User> userList = userService.list();
      return userSayService.list().stream().peek(item -> {
         User user = userList.stream()
            .filter(t -> Objects.equals(t.getId(), item.getUserId()))
            .findFirst()
            .orElse(new User());
         item.setUserName(user.getName());
      }).collect(Collectors.toList());
   }

   @PostMapping
   public Boolean add(@RequestBody UserSay userSay) {
      return userSayService.save(userSay);
   }

   @PutMapping
   public Boolean update(@RequestBody UserSay userSay) {
      return userSayService.updateById(userSay);
   }

   @DeleteMapping("/{id}")
   public Boolean delete(@PathVariable Long id) {
      return userSayService.removeById(id);
   }
}
